/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.timer;

import java.awt.MenuItem;
import java.awt.image.BufferedImage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.plugin.AbstractPlugin;
import org.mopore.jat.plugin.Email;
import org.mopore.jat.plugin.IPluginContext;
import org.mopore.jat.plugin.IPluginFileAccess;
import org.mopore.jat.plugin.PluginException;
import org.mopore.jat.util.ImageService;
import org.mopore.jat.util.MetaInformationService;
import org.mopore.jat.util.Version;

public class TimerPlugin extends AbstractPlugin {
	
	private final Log log = LogFactory.getLog(getClass().getName());
	
	private boolean runCondition;
	

	@Override
	protected Email getAuthorsEmailImpl() throws Exception{
		return MetaInformationService.readProjectHeadEmail();
	}

	@Override
	protected String getAuthorsNameImpl() {
		return MetaInformationService.readProjectHeadName();
	}

	@Override
	public String getDisplayNameImpl() {
		return "timer";
	}

	@Override
	public BufferedImage getPluginIcon() {
		BufferedImage image = 
			ImageService.load( "/images/popupicons/timer.gif" );
		return image;
	}

	@Override
	protected String getPluginNameImpl() {
		return TimerPlugin.class.getName();
	}

	@Override
	public MenuItem[] getMenuFocusActions() {
		return null;
	}


	@Override
	protected Version getVersionImpl() {
		return MetaInformationService.readVersion();
	}

	@Override
	public boolean isFocusable() {
		return true;
	}

	@Override
	public void onPluginLoadImpl( IPluginContext pluginContext ) throws PluginException {
		// not used
	}

	@Override
	public void onPluginShutDownImpl() throws PluginException {
		log.info( "negate runcondition..." );
		runCondition = false;
	}

	
	@Override
	public void runPlugin(IPluginContext pluginContext) throws InterruptedException {
		
		log.info( "testing fileaccess persist" );
		Class<TimingPersistState> clazz = TimingPersistState.class;
		
		try {
			IPluginFileAccess fileAccess = pluginContext.getFileAccess();
			TimingPersistState state = fileAccess.retrieve( clazz );
			
			if ( state != null ) {
				String name = state.getName();
				log.info( "TimingPersistState retrieved: " + name );
			}
			else {
				log.info( "nothing retrieved. Creating new persistState " );
				state = new TimingPersistState();
				state.setName( "test name" );
				log.info( "persist new persist state" );
				fileAccess.persist( state );
			}
		} catch (Exception e) {
			log.error( "DAMN!", e );
		}
		
		runCondition = true;
		log.info( "now running..." );
		while( runCondition ) {
			Thread.sleep( 1000 );
		}
		log.debug( "run condition does not fit anymore..." );
	}


	@Override
	public MenuItem[] getMenuActions(MenuItem base) {
		
		return new MenuItem[] {
			new MenuItem( "manage count downs" ),
			new MenuItem( "invoke current" ),
			new MenuItem( "stop current" ),
		};
	}

}
